﻿using System;
using System.ComponentModel;
using System.Windows.Threading;

namespace MineSweeping.AppCode
{
    public class GameContext : INotifyPropertyChanged
    {
        public int Rows { get; private set; }

        public int Cols { get; private set; }

        public int MineCount { get; private set; }

        public Difficulty Difficulty { get; private set; }

        private int blockLeftCount;

        public int BlockLeftCount
        {
            get { return blockLeftCount; }
            set
            {
                blockLeftCount = value;
                IsWin = (BlockLeftCount == MineCount);
            }
        }

        private bool isFlaging;

        public bool IsFlaging
        {
            get { return isFlaging; }
            set
            {
                isFlaging = value;

                NotifyPropertyChanged("IsFlaging");
            }
        }

        private bool isLosing;

        public bool IsLosing
        {
            get { return isLosing; }
            set
            {
                isLosing = value;

                if(isLosing)
                {
                    IsGameRunning = false;
                    StopTimer();
                }

                NotifyPropertyChanged("IsLosing");
            }
        }

        private bool isWin;

        public bool IsWin
        {
            get { return isWin; }
            private set
            {
                isWin = value;

                if (isWin)
                {
                    StopTimer();
                    IsGameRunning = false;
                }

                NotifyPropertyChanged("IsWin");
            }
        }

        private bool isGameRunning;

        public bool IsGameRunning
        {
            get { return isGameRunning; }
            set
            {
                isGameRunning = value;

                NotifyPropertyChanged("IsGameRunning");
            }
        }

        private int mineLeftCount;

        public int MineLeftCount
        {
            get { return mineLeftCount; }
            set
            {
                mineLeftCount = value;

                NotifyPropertyChanged("MineLeftCount");
            }
        }

        private DispatcherTimer timer;

        private int showTime;

        public int ShowTime
        {
            get { return showTime; }
            set
            {
                showTime = value;

                NotifyPropertyChanged("ShowTime");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void InitGameContext(Difficulty diff)
        {
            Difficulty = diff;

            switch (Difficulty)
            {
                case Difficulty.Easy:
                    Rows = 8;
                    Cols = 10;
                    MineCount = 10;
                    break;
                case Difficulty.Medium:
                    Rows = 12;
                    Cols = 12;
                    MineCount = 24;
                    break;
                case Difficulty.Hard:
                    Rows = 22;
                    Cols = 12;
                    MineCount = 45;
                    break;
            }

            MineLeftCount = MineCount;

            BlockLeftCount = Rows*Cols;

            ShowTime = 0;

            IsFlaging = false;
            IsWin = false;
            IsLosing = false;

            IsGameRunning = true;
        }

        public void StartTimer()
        {
            StopTimer();

            // 启动定时器，1分钟刷新一次
            timer = new DispatcherTimer();
            timer.Tick += Timer_Tick;
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Start();
        }

        public void StopTimer()
        {
            if (timer != null)
            {
                timer.Stop();
                timer = null;
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (ShowTime < 999)
            {
                ShowTime++;
            }
        }
    }
}
